﻿using CsvHelper;
using CsvHelper.Configuration;
using CsvHelper.TypeConversion;
using System;
using System.Collections.Generic;
using System.Text;

namespace FireBillBook.Core.Models
{
    public class FundDirectionConverter : DefaultTypeConverter
    {
        private const string ExpensesText = "支出";
        private const string IncomeText = "收入";

        public override object ConvertFromString(string text, IReaderRow row, MemberMapData memberMapData)
        {
            switch (text.Trim())
            {
                case ExpensesText:
                    return FundFlowDirection.Expenses;
                case IncomeText:
                    return FundFlowDirection.Income;
                default:
                    return FundFlowDirection.None;
            }
        }

        public override string ConvertToString(object value, IWriterRow row, MemberMapData memberMapData)
        {
            if(value is FundFlowDirection)
            {
                switch (value)
                {
                    case FundFlowDirection.Expenses:
                        return ExpensesText;
                    case FundFlowDirection.Income:
                        return IncomeText;
                    default:
                        return string.Empty;
                }
            }
            else
            {
               throw new TypeConverterException(this, memberMapData, value, row.Context, $"非指定转换类型'{nameof(FundFlowDirection)}'");
            }
        }
    }
}
